package com.syt.cellphone.information.ui.phone.details

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.MenuItem
import androidx.core.app.NavUtils
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.syt.cellphone.information.R
import com.syt.cellphone.information.base.VMBaseActivity
import com.syt.cellphone.information.data.pojo.*
import com.syt.cellphone.information.utils.getViewModel
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.activity_phone_details.*
import kotlinx.android.synthetic.main.view_details_base_content.*
import org.jetbrains.anko.toast


/**
 *  @author: syt
 *  创建时间: 2020/9/2 17:24
 *  @功能   手机信息详情
 *  @version v1.0
 */
class PhoneDetailsActivity : VMBaseActivity<PhoneDetailsViewModel>(R.layout.activity_phone_details) {

    override val viewModel: PhoneDetailsViewModel
        get() = getViewModel(PhoneDetailsViewModel::class.java)

    // 评价适配器
    private lateinit var estimateAdapter: EstimateAdapter

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        initDrawView()
        initSlide()
        initEstimate()
        actionBar?.setDisplayHomeAsUpEnabled(true)
        viewModel.phoneDetails.observe(this, Observer { showPhoneDetails(it) })
        viewModel.initData(intent)

    }

    override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> NavUtils.navigateUpFromSameTask(this)
            R.id.menu_enshrine -> this.toast("收藏")
            R.id.menu_refresh -> this.toast("刷新")
        }
        return super.onCompatOptionsItemSelected(item)
    }

    private fun initDrawView() {

    }

    private fun initSlide() {
        // 下面滑动监听
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            nestedScrollView_details.setOnScrollChangeListener {
                    v,
                    scrollX,
                    scrollY,
                    oldScrollX,
                    oldScrollY ->
                // 下半部滑动时，直接给标题上名称
                toolbar.title = tv_details_title_name.text
            }
        }
    }

    private fun initEstimate() {
        estimateAdapter = EstimateAdapter(this, object : EstimateAdapter.CallBack {

        })
        rv_details_estimate.layoutManager = LinearLayoutManager(this)
        rv_details_estimate.adapter = estimateAdapter
    }

    private fun showPhoneDetails(phoneDetails: PhoneDetails) {
        showBanner(phoneDetails.photo)
        showBasePhone(phoneDetails.base)
        showConfig(phoneDetails.config)
        showCamera(phoneDetails.camera)
        showShow(phoneDetails.show, phoneDetails.facade, phoneDetails.battery)
        showEstimate(phoneDetails.estimate)
    }

    // 处理轮播图
    private fun showBanner(photos: List<Photo>) {
        if (photos.isEmpty()) {
            // 显示一张占位图

        }
        banner_phone_details_img
            .addBannerLifecycleObserver(this)
            .setAdapter(object : BannerImageAdapter<Photo>(photos) {
                override fun onBindView(
                    holder: BannerImageHolder,
                    data: Photo,
                    position: Int,
                    size: Int
                ) {
                    //图片加载自己实现
                    Glide.with(holder.itemView)
                        .load("https://${data.photoMin}")
                        .apply(RequestOptions.bitmapTransform(RoundedCorners(30)))
                        .into(holder.imageView)
                }
            })
            .indicator = CircleIndicator(this)
    }

    // 标题内容
    private fun showBasePhone(base: PhoneBase) {
        tv_details_title_name.text = base.baseName
        tv_details_title_feature.text = base.baseFeature
        coll.title = base.baseName
    }

    // 增加配置
    private fun showConfig(config: Config) {
        tv_soc_name.text = config.socName
        tv_memory.text = config.configMemory
        tv_network.text = config.configNetworking
        tv_unlock.text = config.configUnlock
        tv_remark.text = config.configRemork
    }

    @SuppressLint("SetTextI18n")
    private fun showShow(show: Show, facade: Facade, battery: Battery) {
        show?.let {
            tv_show_texture.text = show.showTexture + show.showSize + show.showResolratio + show.showArrange + show.showPpi
            tv_show_other.text = show.showQuality + show.showOther
        }
        facade?.let {
            tv_facade_size.text = facade.facadeSize + "\t" + facade.facadeWeight
            tv_facade_other.text = facade.facadeTexture + facade.facadeOther
        }
        battery?.let {
            tv_battery.text = battery.batteryCapacity + battery.batteryScheme
        }
    }

    @SuppressLint("SetTextI18n")
    private fun showCamera(camera: Camera) {
        tv_camera_pixel.text = camera.cameraPixel + " " + camera.cameraAperture
        tv_camera_graph.text = camera.cameraGraph
        tv_camera_other.text = camera.cameraVideo + " " + camera.cameraRemarks
    }

    // 增加显示评价
    private fun showEstimate(estimates : List<Estimate>) {
        estimateAdapter.addItems(estimates)
    }
}